package com.practicesuite.controllayer;

import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import com.practicesuite.systemlayer.Appointment;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Panel for viewing a list of all appointments.
 * 
 * @author James
 *
 */
public class AppointmentListPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    
    //public List<Appointment> appointments = new ArrayList<Appointment>();
    public List<Appointment> appointments;
    public String[] visibleAppointments;
    //public Patient patient1;
    //public Appointment appointment1;
    public Appointment appointment;
    public int appointmentIndex;
    

    /**
     * Create the panel.
     */
    public AppointmentListPanel(MainWindow container, final User user) {
        this.window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        //patient1 = new Patient("Ron", 41, 41, null, "123 Memory Lane", "6781234567", "Male", 22, null, "ron@gatech.edu");
        //appointment1 = new Appointment(0, 30, 11, false, patient1, "Physical therapy", 11);
        //appointments.add(appointment1);
        
        try {
            appointments = SystemDriver.getAppointments();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
        }
        visibleAppointments = new String[appointments.size()];
        for (int i = 0; i < appointments.size(); i++) {
            visibleAppointments[i] = appointments.get(i).getReasonForApp();
        }
        
        JPanel panel = new JPanel();
        add(panel);
        panel.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_1 = new JPanel();
        panel.add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new GridLayout(0, 1, 0, 0));
        panel_1.setPreferredSize(new Dimension(500, 100));
        
        JLabel lblPatientList = new JLabel("Appointment List");
        lblPatientList.setHorizontalAlignment(SwingConstants.CENTER);
        panel_1.add(lblPatientList);
        lblPatientList.setFont(new Font("Tahoma", Font.PLAIN, 24));
        
        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);
        FlowLayout fl_panel_2 = new FlowLayout(FlowLayout.CENTER, 5, 5);
        panel_2.setLayout(fl_panel_2);
        
        final JList appointmentList = new JList(visibleAppointments);
        appointmentList.setFont(new Font("Tahoma", Font.PLAIN, 12));
        appointmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        JScrollPane scrollPane = new JScrollPane(appointmentList);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scrollPane);
        
        JButton btnEditPatientInfo = new JButton("View Appointment Details");
        btnEditPatientInfo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                appointmentIndex = appointmentList.getSelectedIndex();
                try {
                    appointment = appointments.get(appointmentIndex);
                    window.gotoPanel("viewAppointment", appointment, user);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Please select an appointment.");
                }
            }
        });
        panel_2.add(btnEditPatientInfo);
        btnEditPatientInfo.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JPanel panel_3 = new JPanel();
        panel.add(panel_3, BorderLayout.SOUTH);
        panel_3.setPreferredSize(new Dimension(500, 50));
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (user.getEUID() == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (user.getEUID() == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (user.getEUID() == 2) {
                    window.gotoPanel("nurseHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_3.add(btnReturnToHome);

    }

}
